﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Win32;

namespace Quellenverwaltung.Word
{
    class Configuration
    {
        private static Configuration configuration;

        public int WebcamDeviceNumber { get; set; }

        public string SRUServiceURL { get; set; }
        public string SRURecordsSchema { get; set; }
        public string SRUSearchAllTerm { get; set; }
        public string SRUServiceVersion { get; set; }
        public int SRUServiceStartRecord { get; set; }
        public int SRUServiceMaximumRecords { get; set; }
        public Boolean BarcodeReaderTryHarder { get; set; }

        public String[] Tags_Author { get; set; }
        public String Tags_Author_AsString { get { return ConvertStringArrayToString(Tags_Author); } set { Tags_Author = ConvertStringToStringArray(value); } }
        public String[] Tags_SiteName { get; set; }
        public String Tags_SiteName_AsString { get { return ConvertStringArrayToString(Tags_SiteName); } set { Tags_SiteName = ConvertStringToStringArray(value); } }
        public String[] Tags_Title { get; set; }
        public String Tags_Title_AsString { get { return ConvertStringArrayToString(Tags_Title); } set { Tags_Title = ConvertStringToStringArray(value); } }
        public String[] Tags_Url { get; set; }
        public String Tags_Url_AsString { get { return ConvertStringArrayToString(Tags_Url); } set { Tags_Url = ConvertStringToStringArray(value); } }
        public String[] Tags_Publisher { get; set; }
        public String Tags_Publisher_AsString { get { return ConvertStringArrayToString(Tags_Publisher); } set { Tags_Publisher = ConvertStringToStringArray(value); } }
        public String[] Tags_Date { get; set; }
        public String Tags_Date_AsString { get { return ConvertStringArrayToString(Tags_Date); } set { Tags_Date = ConvertStringToStringArray(value); } }
        public String[] Tags_SplitTitleToSiteName { get; set; }
        public String Tags_SplitTitleToSiteName_AsString { get { return ConvertStringArrayToString(Tags_SplitTitleToSiteName); } set { Tags_SplitTitleToSiteName = ConvertStringToStringArray(value); } }


        private Configuration()
        {
            configuration = this;
            if (String.IsNullOrEmpty(readRegistryKey("SRUServiceURL")))
            {
                setDefaults();
                save();
            }
            else
            {
                readAllValues();
            }
        }

        private void readAllValues()
        {
            WebcamDeviceNumber = Int32.Parse(readRegistryKey("WebcamDeviceNumber")); ;

            SRUServiceURL = readRegistryKey("SRUServiceURL");
            SRUSearchAllTerm = readRegistryKey("SRUSearchAllTerm");
            BarcodeReaderTryHarder = Boolean.Parse(readRegistryKey("BarcodeReaderTryHarder"));
            SRURecordsSchema = readRegistryKey("SRURecordsSchema");
            SRUServiceVersion = readRegistryKey("SRUServiceVersion");
            SRUServiceStartRecord = Int32.Parse(readRegistryKey("SRUServiceStartRecord"));
            SRUServiceMaximumRecords = Int32.Parse(readRegistryKey("SRUServiceMaximumRecords"));

            Tags_Author = readRegistryKeyAsArray("Tags_Author");
            Tags_SiteName = readRegistryKeyAsArray("Tags_SiteName");
            Tags_Title = readRegistryKeyAsArray("Tags_Title");
            Tags_Url = readRegistryKeyAsArray("Tags_Url");
            Tags_Publisher = readRegistryKeyAsArray("Tags_Publisher");
            Tags_Date = readRegistryKeyAsArray("Tags_Date");
            Tags_SplitTitleToSiteName = readRegistryKeyAsArray("Tags_SplitTitleToSiteName");
        }

        public void setDefaults()
        {
            WebcamDeviceNumber = 0;
            SRUServiceURL = "http://sru.gbv.de/gvk";
            SRUSearchAllTerm = "pica.all";
            BarcodeReaderTryHarder = false;
            SRURecordsSchema = "mods";
            SRUServiceVersion = "1.1";
            SRUServiceStartRecord = 1;
            SRUServiceMaximumRecords = 75;

            Tags_Author = new String[] { "author", "article:author", "DC.creator" };
            Tags_SiteName = new String[] { "og:site_name", "DC.creator" };
            Tags_Title = new String[] { "DC.title", "fulltitle", "og:title", "twitter:title", "title" };
            Tags_Url = new String[] { "DC.identifier::DCTERMS.URI", "dcterms.identifier::dcterms.uri", "og:url", "twitter:url", "url" };
            Tags_Publisher = new String[] { "DC.publisher", "publisher" };
            Tags_Date = new String[] { "date", "dc.date::dc.w3cdtf", "article:modified_time", "article:published_time" };
            Tags_SplitTitleToSiteName = new String[] { " | ", " - ", " : " };
        }

        public void save()
        {
            saveRegistryKey("WebcamDeviceNumber", WebcamDeviceNumber + "");

            saveRegistryKey("SRUServiceURL", SRUServiceURL);
            saveRegistryKey("SRUSearchAllTerm", SRUSearchAllTerm);
            saveRegistryKey("BarcodeReaderTryHarder", BarcodeReaderTryHarder + "");
            saveRegistryKey("SRURecordsSchema", SRURecordsSchema);
            saveRegistryKey("SRUServiceVersion", SRUServiceVersion);
            saveRegistryKey("SRUServiceStartRecord", SRUServiceStartRecord + "");
            saveRegistryKey("SRUServiceMaximumRecords", SRUServiceMaximumRecords + "");

            saveRegistryKey("Tags_Author", Tags_Author);
            saveRegistryKey("Tags_SiteName", Tags_SiteName);
            saveRegistryKey("Tags_Title", Tags_Title);
            saveRegistryKey("Tags_Url", Tags_Url);
            saveRegistryKey("Tags_Publisher", Tags_Publisher);
            saveRegistryKey("Tags_Date", Tags_Date);
            saveRegistryKey("Tags_SplitTitleToSiteName", Tags_SplitTitleToSiteName);
        }

        public static Configuration getConfiguration()
        {
            if (configuration == null)
                configuration = new Configuration();
            return configuration;
        }

        private void saveRegistryKey(string key, string[] value)
        {
            saveRegistryKey(key, ConvertStringArrayToString(value));
        }

        private void saveRegistryKey(string key, string value)
        {
            try
            {
                RegistryKey regKey = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Nils Mosbach\Literaturrecherche für Word");
                regKey.SetValue(key, value);
                regKey.Close();
            }
            catch
            {
                // ignore
            }
        }

        private string readRegistryKey(string key)
        {
            try
            {
                RegistryKey regKey = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\Nils Mosbach\Word Quellenverwaltung");
                string result = (string)regKey.GetValue(key);
                regKey.Close();
                return result;
            }
            catch
            {
                return "";
            }
        }

        private string[] readRegistryKeyAsArray(string key)
        {
            return ConvertStringToStringArray(readRegistryKey(key));
        }

        private string ConvertStringArrayToString(string[] array)
        {
            string result = string.Join(";", array);
            return result;
        }

        private string[] ConvertStringToStringArray(string str)
        {
            string[] result = str.Split(';');
            return result;
        }

    }
}
